$(function(){
	    $("#form-user-assign_app").validate({
        onkeyup:false,
        focusCleanup:true,
        success:"",
        submitHandler:function(form){
        	var appIdStr = getSelectAppId();
        	var options = {
                type: $(form).attr("method"),
                url: $(form).attr("action"),
                success : formResponse,
                data : {
                	"appIdArr" : appIdStr
                },
                dataType: 'json',  
                error : function(xhr, status, err) {
                }  
            };   
            $(form).ajaxSubmit(options);
        }
    });
});

//获取选择的应用ID列表，逗号拼接成字符串
function getSelectAppId() {
    var selApps = $("input[name='appId']:checked");
    var appIdStr = "";
    if(selApps != undefined && selApps.length > 0){
    	for(var i = 0; i < selApps.length; i++){
    		var appId = $(selApps[i]).val();
    		appIdStr += appId;
    		if(i < selApps.length - 1){
    			appIdStr += ",";
    		}
    	}
    }
    return appIdStr;
}

/*表单回调函数*/
function formResponse(responseText, statusText){
    if(responseText.code == 200){
        layer.msg("成功分配应用,2秒后自动关闭窗口", {
            shift: 2,
            time : 2000,
            icon: 6
        },function(){
        	//关闭弹出框
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });
        
        $("#searchBtn", parent.document).click();
    }else{
        layer.msg(responseText.msg, {
            shift: 2,
            icon: 5
        });
        return false;
    }
}
